package com.common.excel.listener;

import com.common.base.DictionaryAware;
import com.common.entity.personal.PersonalEntity;
import com.common.excel.ExcelListener;
import com.common.excel.ExcelValueCheckResp;
import com.common.excel.excelinfo.CheckEvaluateExcelInfo;
import com.common.repository.evaluate.CheckEvaluateRepository;
import com.common.repository.judicialpersonal.PersonalRepository;
import com.common.service.system.DictionaryService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 版权片单 Excel Listener
 *
 * @author johnny
 * @create 2020-03-10 下午04:07
 **/
@Slf4j
@Getter
@Service
public class CheckEvaluateExcelListener extends ExcelListener<CheckEvaluateExcelInfo> {


    private final CheckEvaluateRepository checkEvaluateRepository;

    private final DictionaryService dictionaryService;

    private final PersonalRepository personalRepository;


    public CheckEvaluateExcelListener(CheckEvaluateRepository checkEvaluateRepository, DictionaryService dictionaryService, PersonalRepository personalRepository) {
        this.checkEvaluateRepository = checkEvaluateRepository;
        this.dictionaryService = dictionaryService;
        this.personalRepository = personalRepository;
    }

    @Override
    protected boolean preProcess(CheckEvaluateExcelInfo checkEvaluateExcelInfo, Integer rowNumber, Map<Integer, List<ExcelValueCheckResp.ErrorInfoResp>> checkRespMap) {

        boolean pass = true;
        //判断Code 是否已经存在
        if (!DictionaryAware.CHECK_RANK_MAP.containsKey(checkEvaluateExcelInfo.getCheckRank())) {
            String dicMap = DictionaryAware.CHECK_RANK_MAP.keySet().stream().reduce((s1, s2) -> s1 + "|" + s2).orElse("");
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("字典表里 未找到 考核等级评定 ：" + checkEvaluateExcelInfo.getCheckRank() + " ,【参考值:" + dicMap + " 】"), checkRespMap);
            pass = false;
        } else {
            checkEvaluateExcelInfo.setCheckRankScore(DictionaryAware.CHECK_RANK_MAP.get(checkEvaluateExcelInfo.getCheckRank()));
        }

        if (!DictionaryAware.CHECK_ORDER_MAP.containsKey(checkEvaluateExcelInfo.getCheckOrder())) {
            String dicMap = DictionaryAware.CHECK_ORDER_MAP.keySet().stream().reduce((s1, s2) -> s1 + "|" + s2).orElse("");
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("字典表里 未找到 绩效等次评定 ：" + checkEvaluateExcelInfo.getCheckOrder() + " ,【参考值:" + dicMap + " 】"), checkRespMap);
            pass = false;
        } else {
            checkEvaluateExcelInfo.setCheckOrderScore(DictionaryAware.CHECK_ORDER_MAP.get(checkEvaluateExcelInfo.getCheckOrder()));
        }

        if (!DictionaryAware.CHECK_CIVIL_MAP.containsKey(checkEvaluateExcelInfo.getCheckCivil())) {
            String dicMap = DictionaryAware.CHECK_CIVIL_MAP.keySet().stream().reduce((s1, s2) -> s1 + "|" + s2).orElse("");
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("字典表里 未找到 公务员考核等次 ：" + checkEvaluateExcelInfo.getCheckCivil() + " ,【参考值:" + dicMap + " 】"), checkRespMap);
            pass = false;
        } else {
            checkEvaluateExcelInfo.setCheckCivilScore(DictionaryAware.CHECK_CIVIL_MAP.get(checkEvaluateExcelInfo.getCheckCivil()));
        }

        String personalName = checkEvaluateExcelInfo.getPersonalName();
        PersonalEntity personalEntity = personalRepository.findByName(personalName);
        if (personalEntity == null) {
            putErrorInfo(rowNumber, new ExcelValueCheckResp.ErrorInfoResp("个人档案中 未找到 姓名 ：" + checkEvaluateExcelInfo.getPersonalName()), checkRespMap);
            pass = false;
        } else {
            checkEvaluateExcelInfo.setPersonalId(personalEntity.getId());
            checkEvaluateExcelInfo.setDepartmentId(personalEntity.getDepartmentId());
            checkEvaluateExcelInfo.setPersonalEntity(personalEntity);
        }
        return pass;
    }


}